#include "message.hpp"
//#include "abstract.hpp"

int main()
{
    // //问题：BaseMessage是抽象类不能实例化呀！！！
    // ns_rpc::BaseMessage::ptr bm = ns_msg::MessageFactory::Createmsg(ns_fields::Mtype::RPC_REQ);
    // ns_msg::RpcRequest::ptr rq= std::dynamic_pointer_cast<ns_msg::RpcRequest>(bm);

    // //ns_msg::RpcRequest::ptr rq = ns_msg::MessageFactory::Createmsg<ns_msg::RpcRequest>();
    
    
    // rq->SetMethod("add");
    // Json::Value val;
    // val["num1"] = 10;
    // val["num2"] = 20;
    // rq->SetParameters(val);

    // std::string ret = rq->serialize();
    // std::cout << ret << std::endl;


    
    // ns_msg::RpcRequest::ptr rr = ns_msg::MessageFactory::Createmsg<ns_msg::RpcRequest>();

    // //把rq对象的ret数据反序列化到rr的_val中
    // bool rett = rr->unserialize(ret);

    // if(rett == false) return -1;
    // ret = rr->check();
    // if(rett == false) return -1;

    // std::cout << rr->GetMethod() << std::endl;
    // std::cout << rr->GetParameters()["num1"].asInt() << std::endl;
    // std::cout << rr->GetParameters()["num2"].asInt() << std::endl;






    //问题：BaseMessage是抽象类不能实例化呀！！！
    ns_rpc::BaseMessage::ptr bm = ns_msg::MessageFactory::Createmsg(ns_fields::Mtype::SERVICE_RES);
    ns_msg::ServiceResponse::ptr rq= std::dynamic_pointer_cast<ns_msg::ServiceResponse>(bm);

    //ns_msg::RpcRequest::ptr rq = ns_msg::MessageFactory::Createmsg<ns_msg::RpcRequest>();
    

    rq->SetRcode(ns_fields::Rcode::RCODE_OK);
    rq->SetOtype(ns_fields::ServiceOptype::SERVICE_DISCOVER);
    rq->SetMethod("add");
    std::vector<ns_msg::Address> addrs;
    addrs.push_back({"127.0.0.1",8880});
    addrs.push_back({"127.0.0.1",8881});

    rq->SetHosts(addrs);
    

    std::string ret = rq->serialize();
    std::cout << ret << std::endl;


    
    ns_msg::ServiceResponse::ptr rr = ns_msg::MessageFactory::Createmsg<ns_msg::ServiceResponse>();

    //把rq对象的ret数据反序列化到rr的_val中
    bool rett = rr->unserialize(ret);

    if(rett == false) return -1;
    ret = rr->check();
    if(rett == false) return -1;


    std::cout << (int)rr->GetRcode() << std::endl;
    std::cout << (int)rr->GetOtype() << std::endl;
    std::cout << rr->GetMethod() << std::endl;
    
    std::vector<ns_msg::Address> addr = rr->GetHosts();

    for(auto& e : addr)
    {
        std::cout << e.first << std::endl;
        std::cout << e.second << std::endl;
        std::cout << "————————" << std::endl;
    }


        std::string body = "msg->serialize()";
        std::string id = "msg->Getid()";
        size_t id_len = id.size();
        Mtype mtype = Mtype::RPC_REQ;
        size_t total_len = body.size() + 4;
        std::string result;
        result.reserve(total_len);//避免append多次内存申请
        result.append((char*)&total_len,4);
        std::cout << "result:" << result << std::endl; 

        result.append((char*)&mtype,4);
        std::cout << "result:" << result << std::endl; 

        result.append((char*)&id_len,4);
        std::cout << "result:" << result << std::endl; 

        result.append(id);
        std::cout << "result:" << result << std::endl; 

        result.append(body);
    
        std::cout << "result:" << result << std::endl; 
    
}